package cn.wanda.projects.phoendb;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.Random;
public class BaseDao {
    protected static final Log log = LogFactory.getLog(BaseDao.class);
    private static String driver = "org.apache.phoenix.jdbc.PhoenixDriver";
    private static final int MAX_TIME_NUM = 40000000;
    //   private static String url = "jdbc:phoenix:192.168.0.6:2181/hbase";

//   Connection conn = null;
//   PreparedStatement pstmt = null;

//    public BaseDao(String url ) {
//
//        System.out.println("开始建立url"+url);
//
//        try {
//            conn = getconnect(url);
//            log.info("phoenixdb conncet is establish");
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//
//
//    }


    static {
        try {
            log.info("开始连接" + driver);
            Class.forName(driver);

        } catch (ClassNotFoundException e) {
            log.info("org.apache.phoenix.jdbc.PhoenixDriver未找到");
        }


    }

    private int count = 0;

    public static Connection getconnect(String url) throws SQLException {


        System.out.println();
        return DriverManager.getConnection(url);

    }

//
//    public void executeSQL(String preparedSql) {
//
//        try {
//
//
//            pstmt = conn.prepareStatement(preparedSql);
//
//            boolean rs = pstmt.execute();
//            conn.commit();
//
//        } catch (SQLException e) {
//            e.printStackTrace(); // 处理SQLException异常
//        } finally {
//            try {
//                closeAll(conn, pstmt, null);
//
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
//
//
//    }


    public static void closeAll(Connection conn, Statement stmt, ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

    /***
     * This method returns String rowkey
     * @param str The value to be rowkeys
     * @return rowkey string
     */
    public String rowkeys(String str) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            buffer.append(str.charAt(new Random().nextInt(str.length())));

        }

        return buffer.toString();

    }


    public static String sha1(String data) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(data.getBytes());
        StringBuffer buf = new StringBuffer();
        byte[] bits = md.digest();
        for(int i=0;i<bits.length;i++){
            int a = bits[i];
            if(a<0) a+=256;
            if(a<16) buf.append("0");
            buf.append(Integer.toHexString(a));
        }
        return buf.toString();
    }



}

